package com.cgsd.heyu.ui.common;

import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.ViewPager;

import com.android.library.utils.AppManager;
import com.android.library.utils.LogUtils;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.cgsd.heyu.R;
import com.cgsd.heyu.entity.AppUpdateEntity;
import com.cgsd.heyu.entity.CaoGen;
import com.cgsd.heyu.presenter.home.HomeContract;
import com.cgsd.heyu.presenter.home.HomePresenter;
import com.cgsd.heyu.ui.BaseActivity;
import com.cgsd.heyu.ui.BaseFragment;
import com.cgsd.heyu.ui.home.fragment.BrandFragment;
import com.cgsd.heyu.ui.home.fragment.CenterFragment;
import com.cgsd.heyu.ui.home.fragment.TaskAdFragment;
import com.cgsd.heyu.ui.home.fragment.TaskFragment;
import com.cgsd.heyu.ui.home.fragment.TriListFragment;
import com.cgsd.heyu.ui.task.adapter.TabAdapter;
import com.cgsd.heyu.utils.DeviceUtils;
import com.cgsd.heyu.utils.StatusBarUtil;
import com.cgsd.heyu.widgets.ScrollableViewPager;
import com.cgsd.heyu.widgets.dialog.AppUpdateDialog;
import com.cgsd.heyu.widgets.tabbar.NormalTabItem;
import com.cgsd.heyu.widgets.tabbar.TabBar;
import com.pceggs.workwall.util.PceggsWallUtils;

import java.util.ArrayList;
import java.util.List;

//import cn.bmob.v3.BmobQuery;
//import cn.bmob.v3.exception.BmobException;
//import cn.bmob.v3.listener.QueryListener;
import cn.jpush.android.api.JPushInterface;

/**
 * Author: sky on 2021/3/22 17:55
 * Email: xcode126@126.com
 * Desc:
 */
public class TabActivity extends BaseActivity<HomeContract.Presenter>
        implements HomeContract.MainView, LocationListener {

    private ScrollableViewPager mViewPager;
    private TabBar mTabBar;
    private TabAdapter adapter;
    private AppUpdateDialog appUpdateDialog;

    /**
     * 跳转到草根时代界面
     */
    public static final String PHOTOGRAPH = "photograph";
    public static final String HOME = "home";

    public static Intent getIntent(Context context) {
        return getIntent(context, "");
    }

    public static Intent getIntent(Context context, String type) {
        Intent intent = new Intent(context, TabActivity.class);
        intent.putExtra("type", type);
        return intent;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_tab;
    }

    @Override
    protected void initView() {
        new HomePresenter(this);
        mViewPager = findViewById(R.id.mViewPager);
        mTabBar = findViewById(R.id.mTabBar);
//        getTableSchema();
        initTab(true);
        PceggsWallUtils.setAuthorities("com.cgsd.heyu.fileProvider");
        String sdkVersion = TTAdSdk.getAdManager().getSDKVersion();
        String registrationID = JPushInterface.getRegistrationID(this);
        com.apkfuns.logutils.LogUtils.e("sdkVersion " + sdkVersion + "\n" + registrationID);

        if (DeviceUtils.isSimulator(this)) { // 这里把模拟器尽调
            showToast("暂不支持此设备");
            AppManager.getAppManager().finishAllActivity();
        }
    }

    @Override
    protected void initData() {
        mPresenter.getQiniuyunImageToken();
        mPresenter.appUpdate();
    }

    @Override
    protected void setStatusBar() {
        StatusBarUtil.fullBar(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (appUpdateDialog != null) {
            appUpdateDialog.dismiss();
        }
    }

    /**
     * 初始化tab
     */
    private void initTab(boolean isTask) {
        List<BaseFragment> list = new ArrayList<>();
        list.add(new TaskAdFragment());
        list.add(new TriListFragment());//视频
        if (isTask) {
            list.add(TaskFragment.newInstance());//任务
        } else {
            list.add(new BrandFragment());//任务
        }
        list.add(new CenterFragment());

        adapter = new TabAdapter(getSupportFragmentManager(), list);
        mViewPager.setAdapter(adapter);
        mViewPager.setOffscreenPageLimit(list.size());
        mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {

            }
        });

        mTabBar.addItem(new NormalTabItem(this).init(R.drawable.selector_tab_find, "发现"));
        mTabBar.addItem(new NormalTabItem(this).init(R.drawable.selector_tab_video, "视频"));
        mTabBar.addItem(new NormalTabItem(this).init(R.drawable.selector_tab_task, "任务"));
        mTabBar.addItem(new NormalTabItem(this).init(R.drawable.selector_tab_my, "我"));

        mViewPager.setCurrentItem(0);
        mTabBar.setCurrentItem(0);
        mTabBar.setOnTabSelectedListener(new TabBar.OnTabSelectedListener() {
            @Override
            public boolean beforeTabSelected(int position, int prePosition) {
                return false;
            }

            @Override
            public void onTabSelected(int position, int prePosition) {
                if (mViewPager != null && mViewPager.getAdapter() != null) {
                    onTabClick(position, prePosition);
                }
            }

            @Override
            public void onTabUnSelected(int position) {
            }

            @Override
            public void onTabReSelected(int position) {
            }
        });
    }

    /**
     * tab被選中
     *
     * @param position
     * @param prePosition
     */
    public void onTabClick(int position, int prePosition) {
        mViewPager.setCurrentItem(position);
        mTabBar.setCurrentItem(position);
    }

    @Override
    public void onLocationChanged(@NonNull Location location) {
        Log.d("print", location.toString());
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    //    todo 2021/04/05 20：56
    @Override
    public void onProviderEnabled(String s) {

    }

    // todo 2021/04/05 20：56
    @Override
    public void onProviderDisabled(String s) {

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        String type = intent.getStringExtra("type");
        if (PHOTOGRAPH.equals(type)) {
            onTabClick(1, 0);
        }else if (HOME.equals(type)) {
            onTabClick(0, 0);
        }
    }

    @Override
    public void setConfig(String sysType) {

    }

    @Override
    public void setAppUpdate(AppUpdateEntity appUpdateEntity) {
        if (appUpdateEntity == null) {
            return;
        }
        appUpdateDialog = new AppUpdateDialog(this, appUpdateEntity);
    }

    /**
     * 获取指定表的表结构信息
     *
     * @return void
     * @throws
     * @method getTableSchema
     */
    public void getTableSchema() {
        /*BmobQuery<CaoGen> mBmobQuery = new BmobQuery<>();
        showLoading();
        mBmobQuery.getObject("1j29LLLB", new QueryListener<CaoGen>() {
            @Override
            public void done(CaoGen caoGen, BmobException e) {
                if (e == null) {
                    LogUtils.i("sky", "查询成功==" + caoGen.toString());
                    initTab(caoGen.isTask());
                    //                    showToast("Config error tip！");
                } else {
                    LogUtils.i("sky", "查询失败==" + e.toString());
                    showToast("Config error tip！");
                    initTab(false);
                }
                loadSuccess();
            }
        });*/

    }

    public boolean videoIsShow() {
        return mViewPager.getCurrentItem() == 1;
    }
}
